﻿<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="TWS.Cart.Data" %>

<script runat="server">

	void Application_BeginRequest(object sender, EventArgs e)
	{
		string fullOrigionalpath = Request.Url.ToString();
		
		//Load Categories
		CartCatCategoryCollection ssCategoryColl = new CartCatCategoryCollection()
			.OrderByAsc(CartCatCategory.Columns.CategoryOrder).Load();
		
		//Load CartConfigVals
	    CartCatConfigurationParameter ssConfigParam = new CartCatConfigurationParameter(2);

		foreach (var cat in ssCategoryColl)
		{
			if (fullOrigionalpath.Contains(Helper.CreateASPXPage(cat.Description)))
			{
				Context.RewritePath(ssConfigParam.ValueX + "?catId=" + cat.Id);
				break;
			}//Else proceed with normal request.
		}
		//string fullOrigionalpath = Request.Url.ToString();

		//if (fullOrigionalpath.Contains("/Products/Books.aspx"))
		//{
		//    Context.RewritePath("/Products.aspx?Category=Books");
		//}
		//else if (fullOrigionalpath.Contains("/Products/DVDs.aspx"))
		//{
		//    Context.RewritePath("/Products.aspx?Category=DVDs");
		//}
	}
	
    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup	
		string tempPath = Server.MapPath("~/temp/");
		if (Directory.Exists(tempPath))
			Directory.Delete(tempPath, true);
		else
			Directory.CreateDirectory(tempPath);
    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e) 
    {
		string tempPath = Server.MapPath("~/temp/"+Session.SessionID)+"/";
		if (Directory.Exists(tempPath))
			Directory.Delete(tempPath,true);
    }
       
</script>
